package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加购物车
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据：{}", shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询当前菜品或者套餐是否存在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if (cartServiceOne != null) {
            //如果已经存在， 就再原来的基数上再+1
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //如果不存在，添加到购物车，默认数量为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne);
    }


    //减少套餐或者菜品数量
    @Transactional
    @PostMapping("sub")
    //携带参数的可能是Dish_id 或者是Setmeal_id 所以用shoppingCart接收
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //菜品id
        Long dishId = shoppingCart.getDishId();

        //套餐id
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        //减少数量的是菜品
        if (dishId != null) {

            //通过dishid查出购物车菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);

            //查询用户对应的购物车
            queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            ShoppingCart cartDish = shoppingCartService.getOne(queryWrapper);

            cartDish.setNumber(cartDish.getNumber() - 1);
            Integer number = cartDish.getNumber();
            if (number > 0) {
                //对数据进行更新
                shoppingCartService.updateById(cartDish);
            } else if (number == 0) {
                //如果购物车的数量为0， 那么就将菜品从购物车删除
                shoppingCartService.removeById(cartDish.getId());
            } else if (number < 0) {
                return R.error("数据异常");
            }
            return R.success(cartDish);
        }
        //减少数量的是套餐
        if (setmealId != null) {
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
            //查询当前用户对应的购物车
            queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

            ShoppingCart cartSetmeal = shoppingCartService.getOne(queryWrapper);

            cartSetmeal.setNumber(cartSetmeal.getNumber() - 1);
            Integer number = cartSetmeal.getNumber();
            if (number > 0) {
                //更新数据
                shoppingCartService.updateById(cartSetmeal);
            } else if (number == 0) {
                //如果购物车数量为0，那么就将套餐从购物车中删除
                shoppingCartService.removeById(cartSetmeal);
            } else if (number < 0) {
                return R.error("数据异常");
            }
            return R.success(cartSetmeal);
        }
        //假如菜品和套餐都进不去  则返回异常
        return R.error("数据异常");
    }


    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        Long currentId = BaseContext.getCurrentId();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean() {

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(lambdaQueryWrapper);

        return R.success("清空购物车成功");
    }


}
